package com.example.fragmenttest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.fragmenttest.Bean.User;
import com.example.fragmenttest.Fragment.FirstFragment;
import com.example.fragmenttest.Fragment.ThirdFragment;
import com.example.fragmenttest.Fragment.SecondFragment;

import org.litepal.LitePal;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText usnedit,psdedit;
    private DrawerLayout drawerLayout ;
    private LinearLayout main_left ;
    private TextView menutxt1,menutxt2 ,menutxt3,menutxt4,menuusername,menuuserid ;
    private Button tofirst,tosecond,tothird ;
    private FirstFragment firstFragment =new FirstFragment() ;
    private SecondFragment secondFragment =new SecondFragment() ;
    private ThirdFragment thirdFragment =new ThirdFragment() ;
    private Fragment defFragment ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        replaceFragment( firstFragment); //一个实体对象
        getUser();
        initView();//   控件的初始化  实例化
        setListener();//监听器
    }
    private void getUser() {
        List<User> userList = LitePal.findAll(User.class) ;
        //当这个数据库中没有数据才添加，有酒不添加了，避免数据重复
        if (userList.size()==0){
            User user =new User(0,"李光通","123");
            User user1 =new User(1,"李琛","456");
            User user2 =new User(2,"李志勇","789");
            user.save() ;
            user1.save() ;
            user2.save() ;
            //往数据库里面存数据
        }
    }
    private void setListener() {

        /*设置颜色*/
        View.OnClickListener listener=new View.OnClickListener() {
            //把监听器拿出来写,这样就不用把同样功能的控件写多次
            @Override
            public void onClick(View v) {//v:
                switch (v.getId()){
                    case R.id.tofirst :
                    case R.id.menutxt1:
                        initColor();//重置所有颜色,防止点击后一直处于一个颜色
                        tofirst.setBackgroundColor(Color.parseColor(btnChooseColor));
                        menutxt1.setBackgroundColor(Color.parseColor(menuChooseColor));
//                        v.setBackgroundColor(Color.YELLOW);//点击后边、变色
                        replaceFragment(firstFragment);
                        break;
                    case  R.id.tosecond :
                    case R.id.menutxt2:
                        initColor();//重置所有颜色,
                        tosecond.setBackgroundColor(Color.parseColor(btnChooseColor));
                        menutxt2.setBackgroundColor(Color.parseColor(menuChooseColor));
//                        v.setBackgroundColor(Color.BLUE);
                        replaceFragment(secondFragment);
                        break;
                    case  R.id.tothird:
                    case R.id.menutxt3:
                        initColor();//重置所有颜色
                        tothird.setBackgroundColor(Color.parseColor(btnChooseColor));
                        menutxt3.setBackgroundColor(Color.parseColor(menuChooseColor));
                        replaceFragment(thirdFragment);
//                        v.setBackgroundColor(Color.BLUE);
                        break;
                    case R.id.menutxt4:
                        initColor();
                        menutxt4.setBackgroundColor(Color.parseColor(menuChooseColor));
                        Intent intent =new Intent(MainActivity.this,LoginActivity.class);
                        startActivity(intent);
                        finish();
                        break;
                }
                drawerLayout.closeDrawer(main_left);//关闭方法
            }
        };
        tofirst.setOnClickListener(listener);;
        tosecond.setOnClickListener(listener);
        tothird.setOnClickListener(listener);
        menutxt1.setOnClickListener(listener);
        menutxt2.setOnClickListener(listener);
        menutxt3.setOnClickListener(listener);
        menutxt4.setOnClickListener(listener);
        menuuserid.setOnClickListener(listener);
        menuusername.setOnClickListener(listener);

    }


    private void initView() {//实例化
        drawerLayout=findViewById(R.id.drawerlayout);
        main_left=findViewById(R.id.main_left);
        menutxt1=findViewById(R.id.menutxt1);
        menutxt2=findViewById(R.id.menutxt2);
        menutxt3=findViewById(R.id.menutxt3) ;
        menutxt4=findViewById(R.id.menutxt4);
        menuuserid=findViewById(R.id.menuuserid); //管理员
        menuusername=findViewById(R.id.menuusername);//管理员
        tofirst=findViewById(R.id.tofirst);
        tosecond=findViewById(R.id.tosecond);
        tothird=findViewById(R.id.tothird) ;
        initColor();//
        // tofirst.setBackgroundColor(Color.parseColor(btnBackColor));
        // menutxt1.setBackgroundColor(Color.parseColor(menuBackColor));
        tofirst.setBackgroundColor(Color.parseColor(btnChooseColor));
        menutxt1.setBackgroundColor(Color.parseColor(menuChooseColor));


        /*  碎片显示id和名字    */
        SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
        //有值就取前面，没有就取后面，防止取空
        String usn=sp.getString("username","");
        int uid=sp.getInt("userid",-1);
        menuusername.setText("用户名："+usn);
        menuuserid.setText("ID："+uid);


    }
    String btnBackColor="#E6E6FA"; //主页面未点击的时候颜色
    String btnChooseColor="#FFC1C1"; //点击后的颜色
    String menuBackColor="#87CEEB"; //碎片未点击颜色
    String menuChooseColor="#E6E6FA";//

    private void initColor(){//给按钮设置未点击颜色
        //给一个参数view
        tofirst.setBackgroundColor(Color.parseColor(btnBackColor));
        tosecond.setBackgroundColor(Color.parseColor(btnBackColor));
        tothird.setBackgroundColor(Color.parseColor(btnBackColor));
        menutxt1.setBackgroundColor(Color.parseColor(menuBackColor));
        menutxt2.setBackgroundColor(Color.parseColor(menuBackColor));
        menutxt3.setBackgroundColor(Color.parseColor(menuBackColor));
        menutxt4.setBackgroundColor(Color.parseColor(menuBackColor));
    }

    //       传入一个参数
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager =getSupportFragmentManager() ;// 一个事物管理者  获取一个值
        FragmentTransaction transaction=fragmentManager.beginTransaction() ;//提交一个事物transaction
        defFragment=fragment ;//传入的Fragment赋值给defFragment
        transaction.replace(R.id.fragment_container,fragment) ;//用前面事物的替换容器中的事物
        transaction.commit() ;//
    }
}
